#!/bin/sh

if [[ $# -ne 1 ]];
then
    echo "Usage: etrade-research.sh <stock-ticker>"
    exit
fi

export agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
alias curl='curl -k -stderr -A $agent -c cookie 2>&1 >/dev/null'
alias echo='echo -e'
account=`cat password/etrade |awk '{print $1}'`
password=`cat password/etrade |awk '{print $2}'`
cred="USER=$account&PASSWORD=$password"

export submit="countrylangselect=us_english&$cred&TARGET=%2Fe%2Ft%2Faccounts%2Faccountscombo&logon.x=34&logon.y=9"

#home page
curl "https://us.etrade.com/e/t/home" 

#submit login/password
curl -b cookie -e "https://us.etrade.com/e/t/home" -d $submit "https://us.etrade.com/login.fcc" 

#login page
curl -b cookie -e "https://us.etrade.com/e/t/home"  "https://us.etrade.com/e/t/accounts/accountscombo" #> login.html

curl -b cookie -D header "https://us.etrade.com/e/t/invest/markets"  #> market.html

#read location from header
export researchurl=`cat header |grep Location |awk '{print $2}'`
curl -b cookie -D header $researchurl

#fetch P3P cookie in HTTP header
export p3pcookie=`cat header  |grep Cookie |cut -d: -f2 |cut -d';' -f1`

#navigate to research page
curl -b "PNG=1; $p3pcookie" $researchurl #> research.html

#Submit Stock for ratings
curl -b "PNG=1; $p3pcookie" "https://www.etrade.wallst.com/v1/stocks/snapshot/snapshot.asp?symbol=$1&rsO=new" > $1.html

export ReutersLastreport=`grep -A 2 "Reuters Research" $1.html |grep "Last Reported" |cut -d'>' -f2 |cut -d'<' -f1`
export ReutersLastDate=`echo $ReutersLastreport|egrep -o "[0-9]+/[0-9]+/[0-9]+"`
export ReutersLastPrice=`./historyday.sh $1 $ReutersLastDate`
export ReutersRating=`grep -A 2 "Reuters Research" $1.html |egrep -o "[a-z]+.gif"  |cut -d'.' -f1`

export SPLastreport=`grep -A 2 "Standard & Poor" $1.html |grep "Last Reported" |cut -d'>' -f2 |cut -d'<' -f1`
export SPLastDate=`echo $SPLastreport|egrep -o "[0-9]+/[0-9]+/[0-9]+"`
export SPLastPrice=`./historyday.sh $1 $SPLastDate`
export SPRating=`grep -A 2 "Standard & Poor" $1.html |egrep -o "[0-5].gif"  |cut -d'.' -f1`

export RochdaleLastreport=`grep -A 2 "Rochdale Research" $1.html |grep "Last Reported" |cut -d'>' -f2 |cut -d'<' -f1`
export RochdaleLastDate=`echo $RochdaleLastreport|egrep -o "[0-9]+/[0-9]+/[0-9]+"`
export RochdaleLastPrice=`./historyday.sh $1 $RochdaleLastDate`
export RochdaleRating=`grep -A 2 "Rochdale Research" $1.html |egrep -o "[a-z]+.gif"  |cut -d'.' -f1`

export SabrientLastreport=`grep -A 2 "Sabrient Research" $1.html |grep "Last Reported" |cut -d'>' -f2 |cut -d'<' -f1`
export SabrientLastDate=`echo $SabrientLastreport|egrep -o "[0-9]+/[0-9]+/[0-9]+"`
export SabrientLastPrice=`./historyday.sh $1 $SabrientLastDate`
export SabrientRating=`grep -A 2 "Sabrient Research" $1.html |egrep -o "[a-z]+.gif"  |cut -d'.' -f1`

export MarketEdgereport=`grep -A 2 "MarketEdge" $1.html |grep "Last Reported" |cut -d'>' -f2 |cut -d'<' -f1`
export MarketEdgeLastDate=`echo $MarketEdgereport|egrep -o "[0-9]+/[0-9]+/[0-9]+"`
export MarketLastPrice=`./historyday.sh $1 $MarketEdgeLastDate`
export MarketEdgeRating=`grep -A 3 "MarketEdge" $1.html |egrep -o "ME[_a-z]+.gif"  |cut -d'.' -f1 |cut -d'_' -f2`

echo "Reuters:\t" 	$ReutersLastreport,$ReutersLastPrice,$ReutersRating 
echo "S&P:\t\t" 	$SPLastreport,$SPLastPrice,$SPRating 
echo "Rochdale:\t"	$RochdaleLastreport,$RochdaleLastPrice,$RochdaleRating 
echo "Sabrient:\t"	$SabrientLastreport,$SabrientLastPrice,$SabrientRating 
echo "Market Edge:\t"	$MarketEdgereport,$MarketLastPrice,$MarketEdgeRating

\rm -f $1.html
